package com.car.loan.framework.aspectj.impl;

import com.car.loan.common.enums.LoanStatusEnum;
import com.car.loan.common.utils.LocalDateUtil;
import com.car.loan.common.utils.StringUtils;
import com.car.loan.framework.aspectj.DataStatusSqlService;
import com.car.loan.project.xr.controller.SettleAccountsController;
import com.car.loan.project.xr.util.bo.business.LoanBaseInfo;
import com.car.loan.project.xr.util.enums.BusinessApostilleStatus;
import com.car.loan.project.xr.util.enums.FileAuditStatus;
import com.car.loan.project.xr.util.enums.LoanBusinessAuditStatus;
import com.car.loan.project.xr.util.enums.PaymentAuditStatus;
import com.car.loan.project.xr.util.enums.PaymentInfoStatus;
import com.car.loan.project.xr.util.enums.PaymentSettleAccountsStatus;
import java.time.LocalDateTime;
import org.springframework.stereotype.Service;

/**
 * @author huangding
 * @description
 * @date 2019/12/7 14:10
 */
@Service
public class SettleAccountStatusSqlServiceImpl implements DataStatusSqlService {

    @Override
    public LoanStatusEnum getLoanStatusEnum() {
        return LoanStatusEnum.SETTLE_ACCOUNTS_STATUS;
    }

    @Override
    public LoanBaseInfo getStatusRemark(LoanBaseInfo loanBaseInfo) {
        Long settleAccountsStatus = loanBaseInfo.getSettleAccountsStatus();
        String statusRemark = StringUtils.EMPTY;
        if (PaymentSettleAccountsStatus.INIT.status == settleAccountsStatus) {
            statusRemark = "待结算";
        } else if (PaymentSettleAccountsStatus.SUBMIT.status == settleAccountsStatus) {
            statusRemark = "已结算";
        }
        loanBaseInfo.setStatusRemark(statusRemark);
        return loanBaseInfo;
    }

    /**
     * 结算信息在提交二个月内可以看，二个月后不能看
     *
     * @param statusAlias 表名
     * @param status 列表状态
     */
    @Override
    public String getSql(String statusAlias, Integer status) {
        LocalDateTime localDateTime = LocalDateTime.now();
        String sql = " ({}.business_audit_status = "
            + LoanBusinessAuditStatus.PASS.status + " or {}.business_apostille_status = "
            + BusinessApostilleStatus.PASS.status + " ) "
            + " and {}.payment_info_status = "
            + PaymentInfoStatus.SUBMIT.status
            + " and (({}.settle_accounts_status = " + PaymentSettleAccountsStatus.SUBMIT.status
            + " and {}.settle_accounts_time <= '" + LocalDateUtil
            .parseDateTimeToString(localDateTime) + "' and  ls.settle_accounts_time >= '"
            + LocalDateUtil
            .parseDateTimeToString(localDateTime.minusMonths(2))
            + "' ) or {}.settle_accounts_status = "
            + PaymentSettleAccountsStatus.INIT.status + " )";
        if (status != null) {
            sql += " and {}.settle_accounts_status =  " + status;
        }
        return sql.replace("{}", statusAlias);
    }

    @Override
    public String getSortSql(String statusAlias) {
        return " order by {}.business_audit_time desc".replace("{}", statusAlias);
    }
}
